package com.alibaba.shopping_manager_api.controller;

import com.alibaba.shopping_common.result.BaseResult;
import com.alibaba.shopping_common.service.FileUploadService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @ClassName FileController
 * @Description TODO
 * @Author Leo
 * @Date 2024/12/10 10:55
 */
@RequestMapping("/file")
@RestController
public class FileController {

    @DubboReference
    private FileUploadService fileUploadService;

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/uploadImage")
    public BaseResult<String> uploadImage(MultipartFile file) throws IOException {
        // 将MultipartFile转化成字节流数组(MultipartFile没有实现序列化接口无法使用dubbo进行远程调用)
        byte[] bytes = file.getBytes();
        String path = fileUploadService.uploadFile(bytes, file.getOriginalFilename());
        return BaseResult.OK(path);
    }

    /**
     * 删除文件
     * @param filePath 文件的存储路径
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult<String> delete(String filePath){
        fileUploadService.deleteFile(filePath);
        return BaseResult.OK(filePath);
    }
}




